package com.buddy.hadoop.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;

import java.io.IOException;

/**
 * 获取HDFS集群上所有节点名称
 */
public class GetList {

    public static void main(String[] args) throws IOException {
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);
        DistributedFileSystem hdfs = (DistributedFileSystem) fs;
        DatanodeInfo[] datanodeInfos = hdfs.getDataNodeStats();
        String[] names = new String[datanodeInfos.length];
        for (int i = 0;i<datanodeInfos.length;i++){
            names[i] = datanodeInfos[i].getHostName();
            System.out.println("node "+i+" name "+names[i]);
        }
    }
}
